package ThreadDemo.MyThreadPool;

import Timer.MyTimerTask;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:周次煜
 * Date: 2024-07-27
 * Time：15:40
 */


public class MyExecutorService {



    // 用一个阻塞队列来存储
    private  BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(1000);


    //  创建的长度
    public  MyExecutorService(int n) {
        for (int i = 0; i < n ; i++) {
            Thread thread = new Thread(()->{
                try {
                    while(true) {

                        // 开始运行
                        Runnable runnable = queue.take();
                        runnable.run();

                    }

                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });

            thread.start();
        }
    }


    // 递呈任务
    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }

}


class Test{
    public static void main(String[] args) throws InterruptedException {
        MyExecutorService myExecutorService = new MyExecutorService(5);


        for (int i = 0; i < 10; i++) {
            int id = i;
            myExecutorService.submit(()->{
                System.out.println("当前的线程名: "+ Thread.currentThread().getName()+" i为: "+ id );
            });
        }




    }
}


